package com.ypx.ec.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ypx.ec.common.constant.ProductConstant;
import com.ypx.ec.product.dao.AttrAttrgroupRelationDao;
import com.ypx.ec.product.dao.AttrGroupDao;
import com.ypx.ec.product.dao.CategoryDao;
import com.ypx.ec.product.entity.AttrAttrgroupRelationEntity;
import com.ypx.ec.product.entity.AttrGroupEntity;
import com.ypx.ec.product.entity.CategoryEntity;
import com.ypx.ec.product.service.AttrAttrgroupRelationService;
import com.ypx.ec.product.service.CategoryService;
import com.ypx.ec.product.vo.AttrRespVo;
import com.ypx.ec.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ypx.ec.common.utils.PageUtils;
import com.ypx.ec.common.utils.Query;

import com.ypx.ec.product.dao.AttrDao;
import com.ypx.ec.product.entity.AttrEntity;
import com.ypx.ec.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrGroupDao attrGroupDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    CategoryService categoryService;
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId, String attrtype) {
        String key = (String) params.get("key");
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        // key不为空
        wrapper.eq("attr_type",attrtype.equals("base") ?
                ProductConstant.Attr_Enum.ATTR_TYPE_BASE.getCode() : ProductConstant.Attr_Enum.ATTR_TYPE_SALE.getCode());
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) ->
                    obj.eq("attr_id", key).or().like("attr_name", key)
            );
        }
        if (catelogId.longValue() != 0) {
            wrapper.eq("catelog_id", catelogId);

        }



            IPage<AttrEntity> page =
                    this.page(new Query<AttrEntity>().getPage(params),
                            wrapper);
            PageUtils pageUtils=new PageUtils(page);

                List<AttrEntity> record=page.getRecords();
                List<AttrRespVo> ans=record.stream().map((attrEntity )-> {
                    AttrRespVo attrRespVo=new AttrRespVo();
                    BeanUtils.copyProperties(attrEntity,attrRespVo);
                    QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper=
                            new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrEntity.getAttrId());
                    if(attrtype.equals("base")){
                        //--------------------------------------------------------------------------------------
                        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity=attrAttrgroupRelationDao.selectOne(queryWrapper);
                        AttrGroupEntity attrGroupEntity=attrGroupDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());
                        attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                    }
                    CategoryEntity categoryEntity=categoryDao.selectById(attrEntity.getCatelogId());

                    attrRespVo.setCatelogName(categoryEntity.getName());

                    return attrRespVo;
                }).collect(Collectors.toList());
                pageUtils.setList(ans);

            return pageUtils;

    }
    @Transactional
    @Override
    public void saveDetail(AttrVo attr) {
        AttrEntity attrEntity= new AttrEntity();
        BeanUtils.copyProperties(attr,attrEntity);
        baseMapper.insert(attrEntity);
        if(attr.getAttrType().equals(ProductConstant.Attr_Enum.ATTR_TYPE_BASE.getCode())){
            AttrAttrgroupRelationEntity entity=new AttrAttrgroupRelationEntity();
            entity.setAttrId(attrEntity.getAttrId());
            entity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationDao.insert(entity);
        }


    }

    @Override
    public AttrRespVo getAttrInfo(Long attrId) {
        AttrEntity attrEntity=baseMapper.selectById(attrId);
        AttrRespVo attrRespVo=new AttrRespVo();
        BeanUtils.copyProperties(attrEntity,attrRespVo);
        attrRespVo.setCatelogPath(categoryService.findCateLogPath(attrRespVo.getCatelogId()));
        //setGroup
        if(attrRespVo.getAttrType()==ProductConstant.Attr_Enum.ATTR_TYPE_BASE.getCode()){
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("attr_id",attrRespVo.getAttrId());
            AttrAttrgroupRelationEntity aare=attrAttrgroupRelationDao.selectOne(queryWrapper);
            attrRespVo.setAttrGroupId(aare.getAttrGroupId());
        }
        return attrRespVo;
    }

    @Override
    public void update(AttrRespVo attr) {
        AttrAttrgroupRelationEntity entity=new AttrAttrgroupRelationEntity();
        entity.setAttrId(attr.getAttrId());
        entity.setAttrGroupId(attr.getAttrGroupId());
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("attr_id",entity.getAttrId());
        attrAttrgroupRelationDao.update(entity,wrapper);

        AttrEntity attrEntity=new AttrEntity();
        BeanUtils.copyProperties(attr,attrEntity);
        baseMapper.updateById(attrEntity);
    }

    @Override
    public List<AttrEntity> getAttrByGroupId(Long attrGroupId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("attr_group_id",attrGroupId);
        List<AttrAttrgroupRelationEntity> relation=attrAttrgroupRelationDao.selectList(queryWrapper);
        List<AttrEntity> list=relation.stream().map((ele)->{
            return baseMapper.selectById(ele.getAttrId());
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public PageUtils getNoAttrByAttrtype(Map<String, Object> params,Long attrGroupId) {
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
        List<AttrAttrgroupRelationEntity> list=attrAttrgroupRelationDao.selectList(wrapper);
        List<Long> attrIds=list.stream().map((ele)->{
           return ele.getAttrId();
        }).collect(Collectors.toList());
        QueryWrapper<AttrEntity> attrQuery=new QueryWrapper<>();
        AttrGroupEntity groupEntity=attrGroupDao.selectOne(new QueryWrapper<AttrGroupEntity>().eq("attr_group_id",attrGroupId));
        Long catelogId=groupEntity.getCatelogId();
        String key = (String) params.get("key");


        if (!StringUtils.isEmpty(key)) {
            attrQuery.and((obj) ->
                    obj.eq("attr_id", key).or().like("attr_name", key)
            );
        }
        attrQuery.eq("catelog_id",catelogId);
        if(attrIds.size()!=0){
            attrQuery.notIn("attr_id",attrIds);
        }








        IPage<AttrEntity> page =
                this.page(new Query<AttrEntity>().getPage(params),
                        attrQuery);
        PageUtils pageUtils=new PageUtils(page);


        return pageUtils;
    }

    @Override
    public List<Long> selectSearchAttrIds(List<Long> attrIds) {

        return baseMapper.selectSearchAttrIds(attrIds);
    }


}